package DBTool;

import java.sql.*;

public class DB {
    private static final String url="jdbc:mysql:///attdb?useSSL=false&characterEncoding=utf8";
    private static final String user="root";
    private static final String psw="root";

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getconn() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, psw);
        return conn;
    }
    public static ResultSet query(String sql,Object... keys) {
        ResultSet rs = null;
        try {
            Connection conn = getconn();
            PreparedStatement pst = conn.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pst.setObject((i+1),keys[i]);
            }
            rs = pst.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    public static int update(String sql,Object... keys) {
        int num = 0;
        try {
            Connection conn = getconn();
            PreparedStatement pst = conn.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pst.setObject((i+1),keys[i]);
            }
            num = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num;
    }
}
